package com.sundi.ecgeneralize.web;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sundi.ecgeneralize.dto.CouponDto;
import com.sundi.ecgeneralize.dto.ResultEntity;
import com.sundi.ecgeneralize.entity.EcCoupon;
import com.sundi.ecgeneralize.service.IEcCouponService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券信息及规则表 前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/ecCoupon")
@CrossOrigin(origins = "*", allowCredentials = "true")
public class EcCouponController {
    @Autowired
    IEcCouponService ecCouponService;

    Logger logger = LoggerFactory.getLogger(this.getClass());

    //    查询
    @RequestMapping(value = "/conponList", method = RequestMethod.GET)
    public ResultEntity getConponList(CouponDto couponDto) {
        PageHelper.startPage(couponDto.getPageNum(), couponDto.getPageSize());
        List<EcCoupon> ecCoupons = ecCouponService.selectConponList(couponDto);
        PageInfo<EcCoupon> info = new PageInfo<>(ecCoupons);
//        Wrapper<EcCoupon> wrapper = new EntityWrapper<EcCoupon>();
//        List<EcCoupon> ecCoupons = ecCouponService.selectList(wrapper);
//        logger.info(JSON.toJSONString(ecCoupons));
        return ResultEntity.ok(info);
    }


    //    添加
    @RequestMapping(value = "/addCoupon")
    public ResultEntity addCoupon(@RequestBody CouponDto couponDto) {
        System.out.println(couponDto);
        EcCoupon ecCoupon = new EcCoupon();
        BeanUtils.copyProperties(couponDto, ecCoupon);
        if (ecCoupon.getId() == null) {
            ecCoupon.setAddTime(new Date());
        }
        boolean insert = ecCouponService.insert(ecCoupon);
        return ResultEntity.ok(insert);
    }

    //    删除
    @RequestMapping(value = "/delCoupon")
    public ResultEntity del(Integer id) {
        System.err.println(id);
        ecCouponService.updAndDel(id);
        return ResultEntity.ok();
    }

    //回显
    @RequestMapping(value = "/getCoupon")
    public ResultEntity getCoupon(Integer id) {
        EcCoupon coupon = ecCouponService.getCoupon(id);
        System.err.println(coupon);
        return ResultEntity.ok(coupon);
    }
    //    修改
    @RequestMapping(value = "/updCoupon")
    public ResultEntity updCoupon(@RequestBody CouponDto couponDto) {
        System.out.println(couponDto);
        EcCoupon ecCoupon = new EcCoupon();
        BeanUtils.copyProperties(couponDto, ecCoupon);
        if (ecCoupon.getId() != null) {
            ecCoupon.setUpdateTime(new Date());
        }
        boolean b = ecCouponService.updateById(ecCoupon);
        return ResultEntity.ok(b);
    }
}
